﻿using <%=ibll_namespace%>;
using <%=model_namespace%>;
using TaskManage.Web.Models;
using TaskManage.Web.Models.Fileter;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace <%=namespace%>
{
    public class <%=class_name%> : BaseController
    {
        #region 模块

        private const string MenuNo = "";

        private readonly DataResultModel drm;

        private readonly <%=ibll_class_name%> _bll;

        #endregion

        #region 构造函数

        public <%=class_name%>(<%=ibll_class_name%> bll)
        {
            _bll = bll;
            drm = new DataResultModel();
            ModuleNo = MenuNo;
        }

        #endregion

        #region 功能

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize(MenuNo, "Query")]
        public JsonResult Get(<%=datatype.parse(key_type)%> id)
        {
            <%=model_name%> model = _bll.Get(id);
            drm.success = true;
            drm.data = model;
            return drm.ToJson();
        }

        /// <summary>
        /// 查询
        /// </summary>
        <%for(var i = 0; i < fields.length; i++) {%>///<param name="<%=fields[i].column_name%>"><%=fields[i].column_comment%></param>
        <%}%>[HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize(MenuNo, "Query")]
        public JsonResult List(<%for(var i = 0; i < fields.length; i++) {%><%=datatype.parse(fields[i].data_type)%> <%=fields[i].column_name%>, <%}%>int? page = 1, int? page_size = 30)
        {
            Tuple<List<<%=model_name%>>, int> result = _bll.ListPagin(<%for(var i = 0; i < fields.length; i++) {%><%=fields[i].column_name%>, <%}%>page.Value, page_size.Value);
            drm.success = true;
            drm.data = result.Item1;
            drm.total = result.Item2;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize(MenuNo, "Add")]
        public JsonResult Insert(<%=model_name%> model)
        {
            model.creater = AuthencationHelper.GetLoginUserNo(User);
            bool success = _bll.Insert(model);
            drm.success = success;
            drm.data = model;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize(MenuNo, "Edit")]
        public JsonResult Update(<%=model_name%> model)
        {
            model.editer = AuthencationHelper.GetLoginUserNo(User);
            bool success = _bll.Update(model);
            drm.success = success;
            drm.data = model;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="<%=key%>">键值</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize(MenuNo, "Delete")]
        public JsonResult Delete(<%=datatype.parse(key_type)%> <%=key%>s)
        {
            bool success = _bll.Delete(<%=key%>s.Split(','));
            drm.success = success;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [OperateAuthorize(MenuNo, "Export")]
        public FileResult Export(<%for(var i = 0; i < fields.length; i++) {%><%=datatype.parse(fields[i].data_type)%> <%=fields[i].column_name%><%if(i<fields.length-1){%>,<%}%><%}%>)
        {
            byte[] bytes = _bll.Export(<%for(var i = 0; i < fields.length; i++) {%><%=fields[i].column_name%><%if(i<fields.length-1){%>,<%}%><%}%>);
            return File(bytes, "application/octet-stream", $"导出信息{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }

        #endregion
    }
}
